<?php

include("connect.php");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
function charityExists($charityName, $dblink) {


	$result = mysql_query(sprintf("SELECT `ID` FROM Charity WHERE Name='%s'",  mysql_real_escape_string($charityName)));
	if (!$result) {
	    die('Could not query:' . mysql_error());
	}
	if(mysql_num_rows($result)!=0) {
		$value = mysql_result($result, 0); // outputs third employee's name
	
		if($value != "" && $value != NULL) {
			return $value;
		} else {
			return insertCharity($charityName, $dblink);
		}

	} else {
		return insertCharity($charityName, $dblink);
	}
}

function insertCharity($charityName, $dblink) {


	$query = sprintf("INSERT INTO Charity(`Name`, `ShowCharity`) VALUES ('%s', 1)",  mysql_real_escape_string($charityName));

	$result = mysql_query($query, $dblink);  

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}


return  mysql_insert_id();

}

if($_POST["name"] != "" && $_POST["name"] != NULL) {
$data = $_POST["name"];

$funeralhomeid = charityExists($data, $link);

echo $funeralhomeid;

}

mysql_close($link);
?>
